package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	cache "news001/cahce"
	"news001/models"
	"time"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Create() {
	var err error
	title := c.GetString("title")
	content := c.GetString("content")
	typeId, _ := c.GetInt("type_id")
	cover := c.GetString("cover")
	remark := c.GetString("remark")

	if title == "" || content == "" || typeId == 0 || cover == "" || remark == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 400,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	var news models.News
	news.Title = title
	news.Content = content
	news.TypeID = typeId
	news.Cover = cover
	news.Remark = remark

	if err = news.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_id_%d", news.ID)
	jsonData, _ := json.Marshal(news)
	cache.Rdb.Set(cache.Ctx, key, jsonData, 0)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "添加成功",
	}
	c.ServeJSON()
}
func (c *NewsController) List() {
	//var err error
	keyword := c.GetString("keyword")
	typeId, _ := c.GetInt("type_id")
	fmt.Println(keyword, typeId)
	Page, _ := c.GetInt("page")
	PageSize, _ := c.GetInt("page_size")

	key := fmt.Sprintf("news_list_%s_%d_%d", keyword, typeId, Page) //从缓存中获取
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	//if cache 存在，使用缓存
	//if cache 不存在，查询数据库，写入缓存
	var news models.News
	var list []*models.News

	if cacheData != "" {
		fmt.Println("使用缓存")
		json.Unmarshal([]byte(cacheData), &list)
	} else {
		list, _ = news.List(keyword, Page, PageSize, typeId)
		if len(list) != 0 {
			//序列化
			jsonData, _ := json.Marshal(list)
			cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
		}
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
		"data": list,
	}
	c.ServeJSON()
	return
}

func (c *NewsController) Del() {
	var err error
	id, _ := c.GetInt("id")
	var news models.News
	news.ID = uint(id)

	if err = news.GetNewByID(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	if err = news.Del(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_id_%d", news.ID)
	cache.Rdb.Del(cache.Ctx, key)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
	}
	c.ServeJSON()
	return
}
func (c *NewsController) Update() {
	var err error
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	content := c.GetString("content")
	typeId, _ := c.GetInt("type_id")
	cover := c.GetString("cover")
	remark := c.GetString("remark")
	var news models.News
	news.ID = uint(id)
	news.Title = title
	news.Content = content
	news.TypeID = typeId
	news.Cover = cover
	news.Remark = remark
	if err = news.Update(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  err.Error(),
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("news_id_%d", news.ID)
	jsonData, _ := json.Marshal(news)
	cache.Rdb.Set(cache.Ctx, key, jsonData, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
	}
	c.ServeJSON()
	return
}
